import Vue from 'vue'
import Vuex from 'vuex'
import api from '@/api';
import beg from '@/common/request';
Vue.use(Vuex)


const store = new Vuex.Store({
	state: {
		user: uni.getStorageSync('user_info'),
		token: '',
		isLogin: false,
		storeInfo: {},
		storeId: uni.getStorageSync('storeId'),
		store: uni.getStorageSync('store'),
		siteroot: uni.getStorageSync('siteroot'),
		layout: {
			index: {},
			personalcenter: {},
		},
		config: {
			reasonConfig: {},
			cashieSetting: {},
		},
		menus: uni.getStorageSync('menus'),
	},
	mutations: {
		setUser(state, data) {
			uni.setStorageSync('userId', data.id)
			uni.setStorageSync('user_info', data)
			state.user = data
			if (data.mobile) state.isLogin = true
		},
		setToken(state, data) {
			uni.setStorageSync('token', data)
			state.token = data
		},
		setStoreInfo(state, data) {
			uni.setStorageSync('storeInfo', data)
			uni.setStorageSync('uniacid', data.id)
			state.storeInfo = data
		},
		setStoreId(state, data) {
			uni.setStorageSync('store', data)
			uni.setStorageSync('storeId', data.id)
			state.store = data
			state.storeId = data.id
		},
		setSiteroot(state, data) {
			uni.setStorageSync('siteroot', data)
			state.siteroot = data
		},
		setConfig(state, data) {
			state.config[data.name] = data.data
		},
		setMenus(state, menus) {
			state.menus = menus;
			uni.setStorageSync('menus', menus);
		}
	},
	actions: {
		async getLogin({
			commit,
			state
		}, params = {}) {
			return await new Promise(async (resolve, reject) => {
				if (params.wxBind == 1) {
					uni.request({
						url: this.state.siteroot +
							'/index.php/api/store_uni_app.Index/wechat_bind',
						method: 'post',
						data: {
							openid: params.openid,
							username: params.username,
							password: params.password
						}
					}).then(res => {})
				}
				let res = await beg.request({
					'url': api
						.login, //params.type == 1 ? api.mobelLogin : api.login,
					method: 'POST',
					data: params
				})
				if (res.code == 1) {
					if (res?.data) {
						uni.setStorageSync('adminInfo', res.data.userInfo);
						commit('setToken', res.data.userInfo.token)
						uni.setStorageSync('username', params.username)
						if (uni.getStorageSync('is_keep') == 1) {
							uni.setStorageSync('password', params.password)
						} else {
							uni.removeStorageSync('password')
						}
						beg.request({
							url: '/admin/Index/index'
						}).then(menuRes => {
							if (menuRes?.data) {
								let menus = [];
								menuRes.data.menus.forEach(item => {
									menus.push(item.name);
									if (item.children && Array.isArray(item
											.children) && item.children
										.length >
										0) {
										item.children.forEach(child => {
											menus.push(child.name);
											if (child.children &&
												Array
												.isArray(child
													.children) &&
												child
												.children.length > 0
											) {
												child.children
													.forEach(
														grandChild => {
															menus
																.push(
																	grandChild
																	.name
																);
														});
											}
										});
									}
								});
								commit('setMenus', menus);
								uni.reLaunch({
									url: '/pages/index/index'
								})
							}
						})
						// if (res.data && res.data.user_info) {
						// 	commit('setUser', res.data.user_info)
						// 	resolve()
						// 	if(res.data.user_info.uniacid == 0){
						// 		uni.reLaunch({
						// 			url: '/pages/login/selectStore'
						// 		})
						// 	}else{
						// 		uni.setStorageSync('uniacid', res.data.user_info.uniacid)
						// 		if(res.data.user_info && res.data.user_info.apply){
						// 			commit('setStoreInfo', res.data.user_info.apply)
						// 		}
						// 		uni.reLaunch({
						// 			url: '/pages/login/selectShop'
						// 		})
						// 	}
						// }
					}
				} else {
					reject()
					uni.showToast({
						title: res.data.msg,
						icon: "none"
					});
				}
			})
		},
		async getMenu({
			commit,
			state
		}, params = {}) {
			return await new Promise(async (resolve, reject) => {
				await beg.request({
					url: '/admin/Index/index'
				}).then(menuRes => {
					if (menuRes?.data) {
						let menus = [];
						menuRes.data.menus.forEach(item => {
							menus.push(item.name);
							if (item.children && Array.isArray(item
									.children) && item.children
								.length >
								0) {
								item.children.forEach(child => {
									menus.push(child.name);
									if (child.children &&
										Array
										.isArray(child
											.children) &&
										child
										.children.length > 0
									) {
										child.children
											.forEach(
												grandChild => {
													menus
														.push(
															grandChild
															.name
														);
												});
									}
								});
							}
						});
						commit('setMenus', menus);
					}
				})
			})
		},
	},
})

export default store